package com.mes.service.yzl;

import com.mes.dao.yzl.CustomerOrderMapper;
import com.mes.entity.hpf.SalePlanDetail;
import com.mes.entity.yzl.*;
import com.mes.entity.yzlExtend.ExtendCustomerOrder;
import com.mes.entity.yzlExtend.ExtendProductor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CustomerOrderServiceImpl implements CustomerOrderService {

    @Resource
    public CustomerOrderMapper customerOrderMapper;

    @Override
    public int orderApproveds(List<Integer> list) {
        return customerOrderMapper.orderApproveds(list);
    }

    @Override
    public int closePlan(Integer id) {
        return customerOrderMapper.closePlan(id);
    }

    @Override
    public int countNew(Condition condition) {
        return customerOrderMapper.countNew(condition);
    }

    @Override
    public List<CustomerOrder> listcustomerOrderNew(Condition condition) {
        return customerOrderMapper.listcustomerOrderNew(condition);
    }

    @Override
    public List<Customer> customerList() {
        return customerOrderMapper.customerList();
    }

    @Override
    public List<Productor> carList() {
        return customerOrderMapper.carList();
    }

    @Override
    public void addCentor(ProContactOrder proContactOrder) {
        customerOrderMapper.addCentor(proContactOrder);
    }

    @Override
    public List<SalePlanDetail> customerOrderXq(long id) {
        return customerOrderMapper.customerOrderXq(id);
    }

    @Override
    public ExtendCustomerOrder findOrderById(long id) {
        return customerOrderMapper.findOrderById(id);
    }

    @Override
    public int deleteOrder(long id) {
        return customerOrderMapper.deleteOrder(id);
    }

    @Override
    @Transactional
    public void deletePro(long id) {
        customerOrderMapper.deletePro(id);
    }

    @Override
    public int orderUnApproveds(List<Integer> list) {
        return customerOrderMapper.orderUnApproveds(list);
    }

    //添加一个  没有分开  做事物处理
    @Override
    @Transactional
    public int addCustomerOrder1(CustomerOrder customerOrder, JSONArray jsonArray) throws Exception {
        long add1 = 0;
        long add2 = 0;
        int isSuccess = 0;
        int add2isSuccess = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        add1 = customerOrderMapper.addCustomerOrder(customerOrder);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jObj = (JSONObject) jsonArray.get(i);
            SalePlanDetail salePlanDetail = new SalePlanDetail();
            salePlanDetail.setCustomerOrderId(add1);
            salePlanDetail.setProductorId(jObj.getLong("pid"));
            salePlanDetail.setPlanQ(jObj.getInt("planQ"));
            salePlanDetail.setLastPlanQ(jObj.getInt("planQ"));
            salePlanDetail.setAddOutIsSelect("0");
            salePlanDetail.setFinishDate(new java.sql.Date(simpleDateFormat.parse((String)jObj.get("finishDate")).getTime()));
            add2isSuccess = customerOrderMapper.addSalePlanDetail(salePlanDetail);
            if (add2isSuccess == 0) {//添加失败  置错误值  跳出循环
                add2 = 1;
                break;
            }
        }

        //测试  循环完成将 add2置1
        //add2 = 1;
        if (add1 == 0 || add2 == 1) {
            throw new RuntimeException();
        } else {
            isSuccess = 1;
        }
        return isSuccess;
    }

    //生成销售计划单号
    @Override
    public String customerOrderNumLast() {

        String returnNumber = "";
        String returnNumberDate = "";
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        returnNumberDate = format.format(new Date());
        returnNumber = "CPD" + returnNumberDate + "00001";

        for (int i = 1; i <= 99999; i++) {

            int dedaoNumber = customerOrderMapper.checkNumUnique(returnNumber);
            if (dedaoNumber == 0) {
                break;
            } else {
                switch (String.valueOf(i).length()) {
                    case 1:
                        returnNumber = "CPD" + returnNumberDate + "0000" + i;
                        break;
                    case 2:
                        returnNumber = "CPD" + returnNumberDate + "000" + i;
                        break;
                    case 3:
                        returnNumber = "CPD" + returnNumberDate + "00" + i;
                        break;
                    case 4:
                        returnNumber = "CPD" + returnNumberDate + "0" + i;
                        break;
                    case 5:
                        returnNumber = "CPD" + returnNumberDate + i;
                        break;
                    default:
                        returnNumber = "CPD" + returnNumberDate + "1000" + i;
                        break;
                }
                //returnNumber = returnNumber + "00" + i;//以后改进
            }
        }
        return returnNumber;
    }

    //修改
    @Override
    @Transactional
    public int updateCustomerAll(CustomerOrder customerOrder, JSONArray jsonArray) throws Exception {
    	long add1 = customerOrder.getId();
        long add2 = 0;
        int isSuccess = 0;
        int add2isSuccess = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jObj = (JSONObject) jsonArray.get(i);
            SalePlanDetail salePlanDetail = new SalePlanDetail();
            salePlanDetail.setPlanQ(jObj.getInt("planQ"));
            salePlanDetail.setLastPlanQ(jObj.getInt("planQ"));
            salePlanDetail.setFinishDate(new java.sql.Date(simpleDateFormat.parse((String)jObj.get("finishDate")).getTime()));
            salePlanDetail.setId(jObj.getLong("id"));
        	add2isSuccess = customerOrderMapper.updatePlanDetail(salePlanDetail);
            if (add2isSuccess == 0) {//添加失败  置错误值  跳出循环
                add2 = 1;
                break;
            }
        }
        
        add1 = customerOrderMapper.updateCustomerOrder(customerOrder);

        //测试  循环完成将 add2置1
        //add2 = 1;
        if (add1 == 0 || add2 == 1) {
            throw new RuntimeException();
        } else {
            isSuccess = 1;
        }
        return isSuccess;
    }

	@Override
	public List<CustomerOrder> listALlCustomerOrde() {
		// TODO Auto-generated method stub
		List<CustomerOrder> cusOrderList = customerOrderMapper.listAllCustomerOrder();
		
		return cusOrderList;
	}

	@Override
	public List<Productor> carPageList(Condition condition) {
		// TODO Auto-generated method stub
		return customerOrderMapper.carPageList(condition);
	}

	@Override
	public int carCount() {
		// TODO Auto-generated method stub
		return customerOrderMapper.carCount();
	}

	@Override
	public void deletePlanDetial(long id) {
		// TODO Auto-generated method stub
		customerOrderMapper.deletePlanDetial(id);
	}

}
